﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using log4net;
using Common;
using AutoFacUtils;
using CashFlow.Db.Entity;
using CashFlow.Admin.Def;
using CashFlow.Admin.Common;
using CashFlow.Admin.Filters;
using CashFlow.Admin.Models;
using CashFlow.Admin.Models.Result;
using CashFlow.Admin.Service;
using CashFlow.Admin.Validator;

namespace CashFlow.Admin.Controllers.Ordinary
{
    public class OrderController : Controller
    {
        private static ILog _logger = LogManager.GetLogger(typeof(OrderController));
        private IAdminService GetAdminService() { return AutoFacHelper.Get<IAdminService>(); }
        private IUserService GetUserService() { return AutoFacHelper.Get<IUserService>(); }
        private IOrderService GetOrderService() { return AutoFacHelper.Get<IOrderService>(); }

        // GET: Order
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult List(QueryOrderModel model)
        {
            var adminService = GetAdminService();
            var ui = adminService.GetLoginUserInfo();
            var orderService = GetOrderService();
            var ls = orderService.QueryShopAllUsersOrder(ui.ShopManagerUserId
                , model.OrderNo
                , model.Date
                , model.State
                , model.Buyer
                , model.Seller);
            var ls2 = ls.Select(x => Convert(x)).ToList();
            var sum = ls2.Sum(x => x.Amount);
            return MyCommonTool.CreateLayUITableResult(true
                , ""
                , ls2
                , ls2.Count);
        }

        private QueryOrderResultModel Convert(OrderBuySellInfo model)
        {
            return new QueryOrderResultModel
            {
                OrderNo = model.Order.OrderNo,
                ProductName = model.Order.Name,
                Buyer = model.Buyer.Name,
                Seller = model.Seller.Name,
                Amount = model.Order.Amount,
                Time = model.Order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"),
                State = model.Order.State,
                PaymentVoucherImagePath = model.Order.PaymentVoucherImagePath
            };
        }
    }
}